package com.longxin.demoapp.base.service.impl;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.widget.Toast;
import com.longxin.demoapp.base.service.IBaseService;
import java.io.Serializable;

import static android.content.pm.PackageManager.PERMISSION_GRANTED;

/**
 * @author Long
 * @version 1.0
 * @description:
 * @remarks:
 * @title BaseServiceImpl
 * @date 2021/4/6 17:10
 */
public abstract class IBaseServiceImpl implements IBaseService {
    //活动
    protected Activity activity;

    public IBaseServiceImpl(Activity activity) {
        this.activity = activity;
    }

    @Override
    public void changeActivity(Class<?> cls) {
        changeActivity(cls, null);
    }

    @Override
    public <T extends Serializable> void changeActivity(Class<?> cls, T data) {
        Intent intent = new Intent(activity, cls);
        intent.putExtra("data",  data);
        activity.startActivityForResult(intent, 1);
    }

    @Override
    public void initActivity() {

    }

    /**
     * 动态申请权限
     * @param vars
     */
    protected void checkAuthority(String...vars){
        for (String var : vars) {
            //动态权限申请
            if (ContextCompat.checkSelfPermission(activity, var) != PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(activity, new String[]{var}, 1);
                Toast.makeText(activity, "授权成功!", Toast.LENGTH_SHORT).show();
            }
        }
    }
}
